<template>
  <div>
    <el-upload
        class="upload-demo"
        drag
        action="/"
        multiple
        :before-upload="beforeUpload"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
  </div>
</template>
<script>
export default {
  data(){
    return{

    }
  },
  methods:{
    beforeUpload(file){
      console.log(file);
      this.uploadFile(file)
      return false;
    },
    uploadFile (file) {
      console.log(file)
      const formData = new FormData()
      formData.append('file', file)
      return new Promise((resolve, reject) => {
        this.$http({
          url: this.$http.adornUrl('/sys/common/uploadFile'),
          method: 'post',
          data: formData

        }).then(({data}) => {
          if (data && data.code === 0) {
            resolve(data.msg)
          } else {
            this.$message.error('图片上传失败')
            reject('')
          }
        })
      })
    },
  },

}
</script>
<style scoped lang="less"></style>
